package com.diandian.backend.order.controller;

import com.alibaba.fastjson.JSON;
import com.diandian.backend.order.param.*;
import com.diandian.backend.order.service.OrderOnlineDouyinLifeService;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.BaseResult;
import com.diandian.base.boot.starter.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 线上抖音本地生活订单记录表
 */
@RestController
@RequestMapping("/order/online/douyin/life")
public class OrderOnlineDouyinLifeController {

    private static final Logger logger = LoggerFactory.getLogger(OrderOnlineDouyinLifeController.class);

    @Autowired
    private OrderOnlineDouyinLifeService orderOnlineDouyinLifeService;

    /**
     * 分页查询订单数据
     * @param param
     * @return
     */
    @PostMapping("/page")
    public BaseResult page(@RequestBody OrderOnlineDouyinLifePageParam param) {
        param.check();
        return ResultUtil.makeResult(orderOnlineDouyinLifeService.page(param.toDTO()));
    }

    /**
     * 查询详情
     */
    @PostMapping("/detail")
    public BaseResult getDetail(@RequestBody OrderOnlineDouyinLifeDetailParam param) {
        param.check();
        return ResultUtil.makeResult(orderOnlineDouyinLifeService.getDetail(param.toDTO()));
    }

    /**
     * 大批量接收抖音线上订单消息
     * 注：目前仅用于线上批量同步抖音订单数据
     * @return
     * @throws Exception
     */
    @PostMapping("/sych")
    public BaseResult sych(@RequestBody OrderOnlineDouyinLifeAddParam param) {

        logger.info("接收到大批量同步线上订单数据：{}", JSON.toJSONString(param));

        param.check();

        orderOnlineDouyinLifeService.sych(param);

        return new BaseResult(0, "success", null);
    }

    /**
     * 补充线上订单消息
     * 注：有则跳过，无则新增
     * @return
     */
    @PostMapping("/supplement")
    public BaseResult supplement(@RequestBody OrderOnlineDouyinLifeAddSupplementParam param) {

        logger.info("接收到补充性线上订单数据：{}", JSON.toJSONString(param));

        orderOnlineDouyinLifeService.supplement(param);

        return new BaseResult(0, "success", null);
    }

    /**
     * 同步抖音线上订单
     * 注：目前仅用于线上批量同步抖音订单数据
     * @return
     * @throws Exception
     */
    @PostMapping("/sych/one")
    public BaseResult sychOne(@RequestBody OrderOnlineDouyinLifeAddOneParam param) {

        logger.info("接收到线上订单数据：{}", JSON.toJSONString(param));

        param.check();

        orderOnlineDouyinLifeService.sychOne(param);

        return new BaseResult(0, "success", null);
    }

    @PostMapping("/status")
    public BaseResult updateStatus(@RequestBody OrderOnlineDouyinLifeUpdateParamParam param) {

        logger.info("接收到线上订单数据：{}", JSON.toJSONString(param));

        param.check();

        orderOnlineDouyinLifeService.updateStatus(param);

        return new BaseResult(0, "success", null);
    }

    /**
     * 导入抖音线上订单数据
     * @param file
     * @param belong
     * @return
     * @throws Exception
     */
    @PostMapping("/import")
    public BaseResult importOrderData(MultipartFile file, @RequestHeader String belong) {
        if(belong == null || "".equals(belong)) {
            throw new DefaultException("归属标识不能为空");
        }
        return ResultUtil.makeResult(orderOnlineDouyinLifeService.importOrderData(file, belong));
    }
}